﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TLDNs.Views
{

    public class UShortToByteHL : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double v = (double)value;
            ushort v2 = 0;
            if (v > ushort.MaxValue)
                v2 = ushort.MaxValue;
            else if (v < ushort.MinValue)
                v2 = 0;
            else
                v2 = (ushort)v;

            bool IsH = (bool)parameter;
            if (IsH == false)
            {
                return (byte)(v2 & 0xFF);
            }
            else
            {
                return (byte)(v2 >> 8);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return 0;
        }
    }

    public class UShortToBin : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double v = (double)value;
            int v2 = (int)v;
            return PubMod.ValueToBinString(v2);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return 0;
        }
    }
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class ParameterEdit : System.Windows.Controls.UserControl
    {
        public ParameterEdit()
        {
            InitializeComponent();
        }

        private void UI_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var uie = e.OriginalSource as UIElement;
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                uie.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }
        }
    }
}
